﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class NhapTinTuc : Form
    {
        public NhapTinTuc()
        {
            InitializeComponent();
        }
        // insert vao csdl
        private void Them_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || pictureBox1.Image == null)
            {
                label5.Text = "Chưa nhập đầy đủ thông tin";
            }
            else
            {
                
                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "conn");         
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "select * from TINTUC where IDtintuc = @IDtintuc";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDtintuc", textBox1.Text);

                conn.Open();
                SqlDataReader r = cmd.ExecuteReader();
                if (r.Read())
                {
                    label5.Text = "ID da ton tai";
                    r.Close();
                }
                else
                {
                 r.Close();
                    if ((MessageBox.Show("Bạn có muốn thêm dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
                    {                     
                    this.DialogResult = DialogResult.OK;

                    cmd.Parameters.Clear();
                    cmd.CommandText = "insert into TINTUC(IDtintuc,tieude,chitiet,hinhanh,ngaydangtin) values(@IDtintuc,@tieude,@chitiet,@hinhanh,@ngaydangtin)";
                    cmd.Parameters.AddWithValue("@IDtintuc", textBox1.Text);
                    cmd.Parameters.AddWithValue("@tieude", textBox2.Text);
                    cmd.Parameters.AddWithValue("@chitiet", textBox3.Text);
                 
                    cmd.Parameters.AddWithValue("@hinhanh", "Img/"+tenhinh);
                    cmd.Parameters.AddWithValue("@ngaydangtin", DateTime.Today);
                    cmd.ExecuteNonQuery();
                                  
                   // pictureBox1.Image.Save(Application.StartupPath + @"Img/" + tenhinh);
                    }
                    
                }
                conn.Close();
            }
        }


        private string filename;
        string tenhinh;
        private void button1_Click(object sender, EventArgs e)
        {
           
            OpenFileDialog f = new OpenFileDialog();
            if (f.ShowDialog() == DialogResult.OK)
            {
                filename = f.FileName;
                tenhinh = System.IO.Path.GetFileName(f.FileName);// lay ten hinh
                pictureBox1.Image = Image.FromFile(filename);
               
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog s = new SaveFileDialog();
            s.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            s.FileName = tenhinh;
            s.ShowDialog();
            if (s.FileName != "")
            {
                System.IO.FileStream fs = (System.IO.FileStream)s.OpenFile();
                pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                fs.Close();

            }
        }

       
    }
}
